﻿using ProjectScheduling.daos;
using ProjectScheduling.models;

namespace ProjectScheduling
{
    public partial class AddProjectTypeForm : Form
    {
        public static AddProjectTypeForm b = new();
        public Action? action;
        public AddProjectTypeForm()
        {
            InitializeComponent();
        }
        private int Id { get; set; }
        public void FindById(int id)
        {
            var data = ProjectTypeDao.Base.Find(id) ?? new();
            Id = data.Id;
            txt_Name.Text = data.Name??"";
            txt_Describe.Text = data.Describe??"";
            txt_StandardWorkingHours.Value = data.StandardWorkingHours;
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            var obj = new ProjectTypes
            {
                Id = Id,
                Name = txt_Name.Text,
                Describe = txt_Describe.Text,
                StandardWorkingHours = Convert.ToInt32(txt_StandardWorkingHours.Value)
            };
            if (Id == 0)
            {
                obj.CreateTime = DateTime.Now;
                ProjectTypeDao.Base.Add(obj);
            }
            else
            {
                ProjectTypeDao.Base.Update(obj, it => new { it.Name, it.Describe, it.StandardWorkingHours });
            }
            MessageBox.Show("保存成功", "成功提示");
            action?.Invoke();
            Close();
        }
    }
}
